package sunwin.yoms.service.person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.dao.UserDao;
import sunwin.yoms.dao.UserRoleDao;
import sunwin.yoms.domain.person.UserDomain;
import sunwin.yoms.domain.person.UserRoleDomain;
import sunwin.yoms.domain.system.SysFunctionDomain;
import sunwin.yoms.service.comm.YomsDefaultData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mt on 2015-8-10.
 */
@Service
public class RoleService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao roleDao;
    @Autowired
    private UserService userService;

    /**
     * 给角色增加功能
     *
     * @param rid        角色id
     * @param funcDomain 功能对象
     */
    public void addRoleFunc(int rid, SysFunctionDomain funcDomain) throws YomsException {
        long value = 0;
        long roleFunc = 0;
        long funcValue = funcDomain.getSysFuncValue();
        UserRoleDomain domain = this.getRoleById(rid);
        switch (funcDomain.getSysFuncGroup()) {
            case 1:
                roleFunc = domain.getUserRoleFunction0();
                value = (roleFunc & funcValue) == funcValue ? roleFunc : roleFunc + funcValue;
                domain.setUserRoleFunction0(value);
                break;
            case 2:
                value = domain.getUserRoleFunction1() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction1(value);
                break;
            case 3:
                value = domain.getUserRoleFunction2() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction2(value);
                break;
            case 4:
                value = domain.getUserRoleFunction3() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction3(value);
                break;
            case 5:
                value = domain.getUserRoleFunction4() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction4(value);
                break;
            case 6:
                value = domain.getUserRoleFunction5() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction1(value);
                break;
            case 7:
                value = domain.getUserRoleFunction6() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction6(value);
                break;
            case 8:
                value = domain.getUserRoleFunction7() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction7(value);
                break;
            case 9:
                value = domain.getUserRoleFunction8() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction8(value);
                break;
            case 10:
                value = domain.getUserRoleFunction9() + funcDomain.getSysFuncValue();
                domain.setUserRoleFunction9(value);
                break;
        }

        if (roleDao.updateDomain(domain) != 1) {
            throw new YomsException("分配角色权限失败");
        }
    }

    /**
     * 撤销角色功能
     *
     * @param rid        角色id
     * @param funcDomain 功能对象
     */
    public void removeRoleFunc(int rid, SysFunctionDomain funcDomain) throws YomsException {
        long value = 0;
        long roleFunc = 0;
        long funcValue = funcDomain.getSysFuncValue();
        UserRoleDomain domain = this.getRoleById(rid);
        switch (funcDomain.getSysFuncGroup()) {
            case 1:
                roleFunc = domain.getUserRoleFunction0();
                value = (roleFunc & funcValue) == 0 ? roleFunc : roleFunc - funcValue;
                domain.setUserRoleFunction0(value > 0 ? value : 1);
                break;
            case 2:
                value = domain.getUserRoleFunction1() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction1(value > 0 ? value : 1);
                break;
            case 3:
                value = domain.getUserRoleFunction2() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction2(value > 0 ? value : 1);
                break;
            case 4:
                value = domain.getUserRoleFunction3() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction3(value > 0 ? value : 1);
                break;
            case 5:
                value = domain.getUserRoleFunction4() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction4(value > 0 ? value : 1);
                break;
            case 6:
                value = domain.getUserRoleFunction5() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction1(value > 0 ? value : 1);
                break;
            case 7:
                value = domain.getUserRoleFunction6() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction6(value > 0 ? value : 1);
                break;
            case 8:
                value = domain.getUserRoleFunction7() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction7(value > 0 ? value : 1);
                break;
            case 9:
                value = domain.getUserRoleFunction8() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction8(value > 0 ? value : 1);
                break;
            case 10:
                value = domain.getUserRoleFunction9() - funcDomain.getSysFuncValue();
                domain.setUserRoleFunction9(value > 0 ? value : 1);
                break;
        }

        if (roleDao.updateDomain(domain) != 1) {
            throw new YomsException("撤销角色权限失败");
        }
    }

    /**
     * 根据角色id获取角色
     *
     * @param rid
     * @return
     */
    public UserRoleDomain getRoleById(int rid) throws YomsException {
        UserRoleDomain roleDomain = roleDomain = roleDao.getDomainById(rid);
        if (roleDomain == null) {
            throw new YomsException("无法找到指定角色");
        }
        return roleDomain;
    }

    /**
     * 获取数据库中所有用户角色
     *
     * @return
     */
    public List<UserRoleDomain> getRoles() {

        List<UserRoleDomain> userRoleDomains = roleDao.getDomainCollection(null);
        for (UserRoleDomain domain : userRoleDomains) {
            if (domain.getUserRoleId() == 1) {
                userRoleDomains.remove(domain);
                break;
            }
        }

        return userRoleDomains;
    }

    /**
     * 为用户分配角色
     *
     * @param userIds   用户ID集合
     * @param roleValue 为用户分配的角色
     * @throws YomsException 异常消息
     */
    @Transactional
    public void allotUsersToRole(List<Integer> userIds, long roleValue) throws YomsException {
        if (userIds != null && userIds.size() > 0) {
            getRoleByValue(roleValue);

            for (int id : userIds) {
                UserDomain userDomain = userService.getUserById(id);

                setUserRole(userDomain, roleValue, false);
            }
        } else {
            throw new YomsException("分配的用户集不能为空");
        }
    }

    /**
     * 撤销用户角色
     *
     * @param userIds   用户ID集合
     * @param roleValue 为用户分配的角色
     * @throws YomsException 异常消息
     */
    @Transactional
    public void cancelAllotUserToRole(List<Integer> userIds, long roleValue) throws YomsException {
        if (userIds != null && userIds.size() > 0) {
            getRoleByValue(roleValue);

            for (int id : userIds) {
                UserDomain userDomain = userService.getUserById(id);

                setUserRole(userDomain, roleValue, true);
            }
        } else {
            throw new YomsException("分配的用户集不能为空");
        }
    }

    /**
     * 设置用户的角色Value
     *
     * @param userDomain 用户
     * @param roleValue  角色value
     * @param isRemove   是否移除用户的角色
     * @throws YomsException
     */
    public void setUserRole(UserDomain userDomain, long roleValue, boolean isRemove) throws YomsException {
        if (roleValue != 0) {
            if (!isRemove) {
                if ((userDomain.getUserRole() & roleValue) == 0) {
                    userDomain.setUserRole(userDomain.getUserRole() + roleValue);
                }
            } else {
                if ((userDomain.getUserRole() & roleValue) == roleValue) {
                    long value = userDomain.getUserRole() - roleValue;
                    userDomain.setUserRole(value > 0 ? value : 1);
                }
            }
            if (userDao.updateDomain(userDomain) != 1) {
                throw new YomsException("分配权限失败");
            }
        }
    }

    /**
     * 根据角色value获取角色
     *
     * @param roleValue
     * @return 单个角色对象
     * @throws YomsException 获取角色的异常信息
     */
    public UserRoleDomain getRoleByValue(long roleValue) throws YomsException {
        UserRoleDomain roleDomainParam = new UserRoleDomain();
        roleDomainParam.setUserRoleValue(roleValue);

        List<UserRoleDomain> roleDomains = roleDao.getDomainByCondition(roleDomainParam);

        if (roleDomains.size() != 1) {
            throw new YomsException("无法找到指定角色");
        }

        return roleDomains.get(0);
    }

    /**
     * 根据用户ID获取角色集
     * @param uid
     * @return
     */
    public List<UserRoleDomain> getRolesByUser(int uid){
        List<UserRoleDomain> domains = roleDao.getRoleByUserId(uid);
        return domains;
    }
}
